package org.autumn.core.util.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxParserUtil extends DefaultHandler {

	private String elementValue;

	public SaxParserUtil(String xmlFile) {
		
	}

	public void run() {
		SaxParserUtil.displayHeader();
//		getFile(String xmlFile)
//		this.parse();
		this.printData();
	}

	public void getFile(String xmlFile) throws ParserConfigurationException, SAXException {
		try {
			//new BufferedReader(new InputStreamReader(System.in));
			//System.out.print("Enter a xml file name: ");
			// String xmlFile = bf.readLine();

			//String xmlFile = "c://bean.xml";

			//System.out.println(xmlFile);
			
			File file = new File(xmlFile);
			if (file.exists()) {
				// get a factory
				parse(xmlFile);
			}
	
		} catch (IOException ie) {
			ie.printStackTrace();
		}
	}

	private void parse(String xmlFile) throws ParserConfigurationException,
			SAXException, IOException {
		SAXParserFactory spf = SAXParserFactory.newInstance();

		// get a new instance of parser
		SAXParser sp = spf.newSAXParser();

		// parse the file and also register this class for call backs
		// sp.parse(xmlFile, this);
		sp.parse(xmlFile, this);
	}

	// Event Handlers
	@SuppressWarnings("unchecked")
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		elementValue = new String(ch, start, length);
	}

	@SuppressWarnings("unchecked")
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

	}

	/**
	 * Iterate through the list and print the contents
	 */
	public void printData() {
		//
	}

	public void Save() {

		// call the service ...........

	}

	// header
	public static void displayHeader() {
		System.out.println("==========================================");
		System.out.println("	Feed Processor version 1.0");
		System.out.println("==========================================");
	}
}
